package com.c.ctgproject.ui.vehiclelocation

import android.app.Activity
import android.app.ProgressDialog
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.mode.VehicleLocation
import com.c.ctgproject.retrofit.Response
import com.c.ctgproject.utils.DialogUtils
import com.c.ctgproject.utils.StatusColor
import com.c.ctgproject.utils.Utils
import com.c.ctgproject.utils.handlerNull
import com.jaeger.library.StatusBarUtil
import com.yzq.zxinglibrary.android.CaptureActivity
import com.yzq.zxinglibrary.common.Constant
import kotlinx.android.synthetic.main.title.*
import kotlinx.android.synthetic.main.activity_vehiclelocation.*

@Suppress("DEPRECATED_IDENTITY_EQUALS", "NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
class VehicleLocationActivity : AppCompatActivity(), View.OnClickListener {
    private val statusColor: StatusColor = StatusColor()
    private var viewModel: VehicleLocationViewModel? = null
    private var dialogUtils: DialogUtils? = null
    private var progressDialog: ProgressDialog? = null
    private var token: String? = null
    private val REQUEST_CODE_SCAN = 10001
    private var mCarCode: String? = null//车牌号
    private var carId: String? = null//id

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_vehiclelocation)
        StatusBarUtil.setTranslucent(this)
        statusColor.setAndroidNativeLightStatusBar(this, false)
        token = Utils.getShared2(this, "token")
        mCarCode = intent.getStringExtra("carCode")
        setSupportActionBar(toolbar)
        toolbar_title.text = "车辆库位"
        supportActionBar?.setHomeButtonEnabled(true)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowTitleEnabled(false)
        event()

    }

    private fun event() {
        take_photoqr.setOnClickListener(this)
        carSign.isEnabled = false
        progressDialog = ProgressDialog(
            this@VehicleLocationActivity,
            R.style.AppTheme_Dark_Dialog
        )
        progressDialog!!.isIndeterminate = true
        progressDialog!!.setCanceledOnTouchOutside(false)
        progressDialog!!.setCancelable(true)
        dialogUtils = DialogUtils()
        viewModel = ViewModelProvider(
            this, ViewModelProvider.AndroidViewModelFactory(application)
        ).get(VehicleLocationViewModel::class.java)
        viewModel!!.carMove(token!!, mCarCode!!)
        viewModel!!.getresponsedata().observe(this, Observer { response: Response<VehicleLocation> ->
            if (response.status == 0) {
                carId = response.data.carId
                ownerPlate.text = response.data.ownerPlate
                carCode.text = response.data.carCode
                enterTime.text = response.data.enterTime
                enterTime.text = response.data.enterTime
                //是否可拆
                mDetachableCB.isChecked = response.data.detachable == "1"
                mDoSellValueCB.isChecked = response.data.doSellValue == "1"

                location.text = response.data.location.handlerNull()

                if (response.data.carSign == "1") {
                    carSign.isChecked = true
                } else if (response.data.carSign == "2") {
                    carSign.isChecked = false
                }
            } else {
                dialogUtils!!.showTwo(this@VehicleLocationActivity, "提示", response.msg)
            }
        })
        viewModel!!.getPostcarMovedata().observe(this, Observer { response: Response<*> ->
            if (response.status == 0) {
                progressDialog!!.dismiss()
                Toast.makeText(this, response.msg, Toast.LENGTH_LONG).show()
                finish()
            } else {
                progressDialog!!.dismiss()
                dialogUtils!!.showTwo(this@VehicleLocationActivity, "提示", response.msg)
            }
        })
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onClick(v: View?) {
        if (v == take_photoqr) {
            //移库
            val intent = Intent(this@VehicleLocationActivity, CaptureActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_SCAN)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        // 扫描二维码/条码回传
        if (requestCode === REQUEST_CODE_SCAN && resultCode === Activity.RESULT_OK) {
            if (data != null) {
                val warehouseId: String = data.getStringExtra(Constant.CODED_CONTENT)
                val strs: List<String> = warehouseId.split(":")
                Log.e("库位id", strs[1])
                Log.e("id", carId!!)
                if (strs.size <= 1) {
                    Toast.makeText(this, "二维码不包含库位信息！", Toast.LENGTH_LONG).show()
                } else {
                    if (strs[0].replace("\uFEFF", "") == "warehouseId") {
                        progressDialog!!.setMessage("正在移库...")
                        progressDialog!!.show()
                        viewModel!!.PostcarMove(token!!, carId!!, strs[1])
                    } else {
                        Toast.makeText(this, "此二维码为车牌信息！", Toast.LENGTH_LONG).show()
                    }
                }


            }
        }
    }

}